package jin.si.controller;

import com.google.gson.Gson;
import io.netty.handler.timeout.ReadTimeoutException;
import jin.si.dao.HomePageMapper;
import jin.si.pojo.*;
import jin.si.service.HomePageService;
import jin.si.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:HomePage
 * Package:jin.si.dao
 * Description:
 *
 * @Date:2022/3/4 9:53
 * @Author: XJR
 */
@RequestMapping("/HomePage")
@RestController
public class HomePageController{

    @Autowired
    private HomePageService service;

    @Value("fdfspath")
    private String path;

    @Qualifier
    private  Jedis jedis =new Jedis("192.168.188.100",6379);

    //查询轮播图
    @RequestMapping("/selectJsCarousel")
    public JsonData selectJsCarousel(Integer display) throws ReadTimeoutException {
        List<JsCarousel> jsCarousels = service.selectJsCarousel(display);
        return jsCarousels!=null?JsonData.buildSuccess(jsCarousels):JsonData.buildError("服务器发生错误");
    };

    //查询课程
    @RequestMapping("/selectJsCourse")
    public JsonData selectJsCourse(Integer courseStatus,Integer orderStatus,Integer orderType){
        Gson gson = new Gson();
        String selectJsCourse = jedis.get("selectJsCourse");
        List<JsCourse> list = gson.fromJson(selectJsCourse, List.class);
        if (list==null){
            List<JsCourse> jsCourses = service.selectJsCourse(courseStatus,orderStatus,orderType);
            String selectJsCours = jedis.get("selectJsCourse");
            List listt = gson.fromJson(selectJsCours, List.class);
            return listt!=null?JsonData.buildSuccess(listt):JsonData.buildError("服务器发生错误");
        }else{
            return list!=null?JsonData.buildSuccess(list):JsonData.buildError("服务器发生错误");
        }
    };

    //通过id查看课程详情
    @RequestMapping("/selectJsCourseById")
    public JsonData selectJsCourseById(Integer courseId){
        System.out.println(courseId);
        JsCourse jsCourse = service.selectJsCourseById(courseId);
        return jsCourse!=null?JsonData.buildSuccess(jsCourse):JsonData.buildError("服务器发生错误");
    };

    //查看课程分类
    @RequestMapping("/selectJsCourseTypeById")
    public JsonData selectJsCourseTypeById(Integer id,Integer courseStatus){
        List<JsCourse> jsCourse = service.selectJsCourseTypeById(id, courseStatus);
        return jsCourse!=null?JsonData.buildSuccess(jsCourse):JsonData.buildError("服务器发生错误");
    };

    //查看课程评论
    @RequestMapping("/selectJsCourseComment")
    public JsonData selectJsCourseComment(JsCommentLike jsCommentLike){
        List<JsCourse> jsCourses = service.selectJsCourseComment(jsCommentLike);
        return jsCourses!=null?JsonData.buildSuccess(jsCourses):JsonData.buildError("服务器发生错误");
    };

    //查看课程销量
    @RequestMapping("/selectJsCourseSalesVolume")
    public JsonData selectJsCourseSalesVolume(Integer orderStatus,Integer productId,Integer userId){
        Integer selectJsCourseSalesVolume = service.selectJsCourseSalesVolume(orderStatus, productId,userId);
        return selectJsCourseSalesVolume!=null?JsonData.buildSuccess(selectJsCourseSalesVolume):JsonData.buildError("服务器发生错误");
    };

    //查看课程点赞数
    @RequestMapping("/selectJsCourseLikeNum")
    public JsonData selectJsCourseLikeNum(Integer type,Integer courseId,Integer userId){
        Integer selectJsCourseLikeNum = service.selectJsCourseLikeNum(type, courseId,userId);
        return selectJsCourseLikeNum!=null?JsonData.buildSuccess(selectJsCourseLikeNum):JsonData.buildError("服务器发生错误");
    };
    //查看课程收藏量
    @RequestMapping("/selectJsCourseCollectNum")
    public JsonData selectJsCourseCollectNum(Integer type,Integer courseId,Integer userId){
        Integer selectJsCourseCollectNum = service.selectJsCourseCollectNum(type, courseId,userId);
        return selectJsCourseCollectNum!=null?JsonData.buildSuccess(selectJsCourseCollectNum):JsonData.buildError("服务器发生错误");
    };
    //查看课程分享数量
    @RequestMapping("/selectJsCourseShareNum")
    public JsonData selectJsCourseShareNum(Integer type,Integer courseId){
        Integer selectJsCourseShareNum = service.selectJsCourseShareNum(type, courseId);
        return selectJsCourseShareNum!=null?JsonData.buildSuccess(selectJsCourseShareNum):JsonData.buildError("服务器发生错误");
    };

    //添加课程点赞量
    @RequestMapping("/insertCourseLikeNum")
    public JsonData insertCourseLikeNum(JsFabulous jsFabulous){
        Boolean insertCourseLikeNum = service.insertCourseLikeNum(jsFabulous);
        return insertCourseLikeNum!=null?JsonData.buildSuccess(insertCourseLikeNum):JsonData.buildError("服务器发生错误");
    };
    //删除课程点赞量
    @RequestMapping("/delCourseLikeNum")
    public JsonData delCourseLikeNum(JsFabulous jsFabulous){
        Boolean delCourseLikeNum = service.delCourseLikeNum(jsFabulous);
        return delCourseLikeNum!=null?JsonData.buildSuccess(delCourseLikeNum):JsonData.buildError("服务器发生错误");
    };

    //添加课程收藏量
    @RequestMapping("/insertCourseCollectNum")
    public JsonData insertCourseCollectNum(JsUserCollection jsUserCollection){
        Boolean insertCourseCollectNum = service.insertCourseCollectNum(jsUserCollection);
        return insertCourseCollectNum!=null?JsonData.buildSuccess(insertCourseCollectNum):JsonData.buildError("服务器发生错误");
    };
    //删除课程收藏量
    @RequestMapping("/delCourseCollectNum")
    public JsonData delCourseCollectNum(JsUserCollection jsUserCollection){
        Boolean delCourseCollectNum = service.delCourseCollectNum(jsUserCollection);
        return delCourseCollectNum!=null?JsonData.buildSuccess(delCourseCollectNum):JsonData.buildError("服务器发生错误");
    };

    //添加课程评论
    @RequestMapping("/insertCourseComment")
    public JsonData insertCourseComment(JsComment comment){
        Boolean insertCourseComment = service.insertCourseComment(comment);
        return insertCourseComment!=null?JsonData.buildSuccess(insertCourseComment):JsonData.buildError("服务器发生错误");
    };

    //添加课程评论的点击量
    @RequestMapping("/insertCourseCommentLikeNum")
    public JsonData insertCourseCommentLikeNum(JsCommentLike jsCommentLike){
        Boolean insertCourseCommentLikeNum = service.insertCourseCommentLikeNum(jsCommentLike);
        return insertCourseCommentLikeNum!=null?JsonData.buildSuccess(insertCourseCommentLikeNum):JsonData.buildError("服务器发生错误");
    };

    //删除课程评论的点击量
    @RequestMapping("/deleteCourseCommentLikeNum")
    public JsonData deleteCourseCommentLikeNum(JsCommentLike jsCommentLike){
        Boolean deleteCourseCommentLikeNum = service.deleteCourseCommentLikeNum(jsCommentLike);
        return deleteCourseCommentLikeNum!=null?JsonData.buildSuccess(deleteCourseCommentLikeNum):JsonData.buildError("服务器发生错误");
    };

    //查看当前用户是否点赞课程评论
    @RequestMapping("/selectCourseCommentLikeNum")
    public JsonData selectCourseCommentLikeNum(Integer productId,Integer userId,Integer userType){
        JsCommentLike jsCommentLike = service.selectCourseCommentLikeNum(productId, userId,userType);
        return jsCommentLike!=null?JsonData.buildSuccess(jsCommentLike):JsonData.buildError("服务器发生错误");
    };
}
